<layout name='Layout/layout_main' />
<tagLib name="html" />
<script type="text/javascript">
	function test() {
		form_search.submit();
	};

	function save() {
		$("#node_list").val('');
		$(".sub_left_menu .tree_menu input[name='node_id']:checked").each(function() {
			$("#node_list").val($("#node_list").val() + $(this).val() + ",");
		})
		sendForm("form_data", "{:U('set_node')}");
	};

	function showdata(result){
		$(".sub_left_menu .tree_menu input").each(function(){
			$(this).attr("checked", false);
			$(this).parents("td").removeClass("active");
		});
		for (var s in result.data){
			$(".sub_left_menu .tree_menu input[name='node_id'][value=" + result.data[s].node_id + "]").attr("checked", true);

			if(result.data[s].admin==1){
				$(".sub_left_menu .tree_menu input[name='admin[]'][value=" + result.data[s].node_id + "]").attr("checked", true);			
				$(".sub_left_menu .tree_menu input[name='admin[]'][value=" + result.data[s].node_id + "]").parents("td").addClass("active");
			}
			if(result.data[s].write==1){
				$(".sub_left_menu .tree_menu input[name='write[]'][value=" + result.data[s].node_id + "]").attr("checked", true);
				$(".sub_left_menu .tree_menu input[name='write[]'][value=" + result.data[s].node_id + "]").parents("td").addClass("active");						
			}
			if(result.data[s].read==1){
				$(".sub_left_menu .tree_menu input[name='read[]'][value=" + result.data[s].node_id + "]").attr("checked", true);
				$(".sub_left_menu .tree_menu input[name='read[]'][value=" + result.data[s].node_id + "]").parents("td").addClass("active");							
			}
			$(".sub_left_menu .tree_menu input[node=" + result.data[s].node_id + "]").parents("td").addClass("active");
		};
		$("#org_node_list").val('');
		$(".sub_left_menu .tree_menu input[name='node_id']:checked").each(function() {;
			$("#org_node_list").val($("#org_node_list").val() + $(this).val() + ",");
		})
		$("#opmode").val("edit");
	};

	$(document).ready(function() {
		set_return_url();
		$(".role_list  tr").click(function() {
			$(".role_list  tr").attr("class","");
			$(this).attr("class","active");
			$("#role_id").val($(this).attr("id"));
			sendAjax("{:U('get_node_list')}", "role_id=" + $(this).attr("id"), function(data) {
				showdata(data);
			});
			return false;
		})

		$(".sub_left_menu .tree_menu input").click(function(event) {	
			if ($(this).attr("checked") == "checked") {
				$(this).parents("td").addClass("active");
			} else {
				$(this).parents("td").removeClass("active");				
			}
			event.stopPropagation();
		})


		$(".sub_left_menu .tree_menu  td").click(function() {
			if ($(this).find("input").attr("checked") == "checked") {
				$(this).find("input").attr("checked", false);
				$(this).removeClass("active");
			} else {
				if($(this).find("input").length>0){
					$(this).find("input").attr("checked", true);
					$(this).addClass("active");
				}
			}
		})
	}); 
</script>
<div class="admin" >
	<div class="row-fluid title">
		<span>权限组管理</span>
	</div>
	<form name="form_search" method="post" action="">
		<div class=" row-fluid operate" >
			<div class="left">
				<html:select name="s_pid" options='groupList' selected='pid'  change='test()'/>
			</div>
			<div class="right">
				<a onclick="save()" class="btn">保存</a>
			</div>
		</div>
	</form>
	<div class="row-fluid">
		<div class="span4 role_list">
			<table class="table table-bordered">
				<tr>
					<th>名称</th>
				</tr>
				<volist name="list" id="data">
					<tr id="{$data.id}">
						<td>{$data.name}</td>
					</tr>
				</volist>
			</table>
		</div>
		<div class="span8 sub_left_menu">
			<form id="form_data" method="post" action="">
				<input type="hidden" name="ajax" id="ajax" value="1">
				<input type="hidden" name="role_id" id="role_id">
				<input type="hidden" name="org_node_list" id="org_node_list">
				<input type="hidden" name="node_list" id="node_list">
				<div class="wrap tree_menu">
					<table class="table table-bordered">
						<tr>
							<th style="width:55%;text-align:center">菜单</th>
							<th style="width:15%;text-align:center">读取</th>
							<th style="width:15%;text-align:center">写入/修改</th>
							<th style="width:15%;text-align:center">管理</th>
						</tr>
						<volist name="node_list" id="data">
							<tr >
								<td>
									<i class="icon level{$data['level']+1}"></i>
									<input node="{$data.id}" type="checkbox" name="node_id" value="{$data.id}">{$data.name}</td>
								<if condition="strpos($data['url'],'/index')">
									<td style="text-align:center">
										<input type="checkbox" name="read[]" value="{$data.id}"></td>
									<td  style="text-align:center">
										<input type="checkbox" name="write[]" value="{$data.id}"></td>
									<td  style="text-align:center">
										<input type="checkbox" name="admin[]" value="{$data.id}"></td>
									<else/>
									<td class="check_box text-center">&nbsp;</td>
									<td class="check_box text-center">&nbsp;</td>
									<td class="check_box text-center">&nbsp;</td>
								</if>
							</tr>
						</volist>
					</table>
				</div>
			</form>
		</div>
	</div>
</div>